# -*-coding:utf-8-*-

import csv
import xlsxwriter

# 输出Txt文本
def outputTxt(testResult):
    f = open(u'output/测试问题列表.txt', 'a', 0)
    for index1 in range(len(testResult)):
        for index2 in range(len(testResult[index1])):
            f.write(testResult[index1][index2].encode("gbk") + "  ")
    f.write("\n")
    f.close()

# 输出Csv文件
def outputCsv(testResult):
    csvfile = file(u'output/测试问题列表.csv', 'wb')
    writer = csv.writer(csvfile)
    writer.writerows(
        ["module", "destination", "precondition", "execStep", "expectedResult", "testResult", "problemTypes",
         "priority", "version", "reporter", "agent"])
    rlist = []
    for index1 in range(len(testResult)):
        for index2 in range(len(testResult[index1])):
            rlist.append(testResult[index1][index2].encode("gbk"))
        writer.writerows(rlist)
    csvfile.close()

# 输出Excel文件
def outputExcel(testResult):
    workbook = xlsxwriter.Workbook(u'output/测试问题列表.xlsx')  # 创建一个Excel文件
    worksheet = workbook.add_worksheet()  # 创建一个工作表对象
    worksheet.set_column('A:A', 7.5)
    worksheet.set_column('B:B', 12.5)
    worksheet.set_column('C:C', 27.5)
    worksheet.set_column('D:D', 18.75)
    worksheet.set_column('E:E', 50)
    worksheet.set_column('F:F', 50)
    worksheet.set_column('G:G', 22.5)
    worksheet.set_column('H:H', 10)
    worksheet.set_column('I:I', 7.5)
    worksheet.set_column('J:J', 10)
    worksheet.set_column('K:K', 7.5)
    worksheet.set_column('L:L', 7.5)

    # border：边框，align:对齐方式，bg_color：背景颜色，font_size：字体大小，bold：字体加粗
    top = workbook.add_format({'border': 1, 'align': 'center', 'bg_color': '#00b0f0', 'font_size': 13, 'bold': False})
    green = workbook.add_format({'border': 1, 'align': 'center', 'valign': 'vcenter', 'bg_color': '#cce8cf', 'font_size': 12})
    blue = workbook.add_format({'border': 1, 'align': 'center', 'valign': 'vcenter', 'bg_color': '#5e9cd3', 'font_size': 12})

    worksheet.write('A1', u'模块', top)
    worksheet.write('B1', u'测试功能', top)
    worksheet.write('C1', u'测试目的', top)
    worksheet.write('D1', u'预置条件', top)
    worksheet.write('E1', u'执行步骤', top)
    worksheet.write('F1', u'预期结果', top)
    worksheet.write('G1', u'测试结果', top)
    worksheet.write('H1', u'问题类型', top)
    worksheet.write('I1', u'优先级', top)
    worksheet.write('J1', u'修复版本', top)
    worksheet.write('K1', u'报告人', top)
    worksheet.write('L1', u'经办人', top)

    for index1 in range(len(testResult)):
        for index2 in range(len(testResult[index1])):
            if index1 % 2 == 0:
                worksheet.write(index1 + 1, index2, testResult[index1][index2], green)
            else:
                worksheet.write(index1 + 1, index2, testResult[index1][index2], blue)
    workbook.close()
